﻿using UnityEngine;
using System.Collections;
using Dict.DataModel;
using System.Collections.Generic;
using ProtoBuf.Message;
using System;
namespace UI
{
    public class UIActivityMonthCard : UIActivityTemplate
    {
        #region GameObject
        public GameObject yuekaCanBuy;
        public GameObject yuekaCanReceive;
        public GameObject yuekaReceiveOver;
        public GameObject zhongshenCanBuy;
        public GameObject zhongshenCanReceive;
        public GameObject zhongshenReceiveOver;
        public UILabel canBuyAgain;
        public UILabel canReceiveMonthCard;
        public UILabel canReceiveZhongShen;

        public GameObject yuekaRemainDays;
        public UILabel yuekaRemainDaysLabel;
        public GameObject yuekaConinueBuy;
        #endregion
        #region 变量
        string receiveMonthCard;
        string receiveZhongShen;
       
        #endregion
        public override void Show()
        {
            base.Show();
            AskChargeList();
        }
        void InitLabel()
        {
            canBuyAgain.text = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.CanBuyMonthCard.ToString();
            canReceiveMonthCard.text = receiveMonthCard;
            canReceiveZhongShen.text = receiveZhongShen;
            //CanBuyMonthCard();
            //CanBuyPerment();

            MonthCardRemainDays();

            CanClaimedCard();

        }
        public override void Refersh()
        {
            CanClaimedCard();
        }

        void MonthCardRemainDays()
        {
            long monthCardEndTime = StaticData.playerData.monthCardEndTime;//服务器发来的月卡结束时间
            DateTime endTime = ServerTimerTool.Java2CSTime(monthCardEndTime);
            DateTime currentTime = ServerTimerTool.Java2CSTime(ServerTimerTool.CurrentUTCJavaTime());
            TimeSpan ts = endTime.Subtract(currentTime);//从今天到月卡到期时间还有xxx时间
            if (ts.Days >= 0)
            {
                yuekaRemainDays.SetActive(true);
                yuekaRemainDaysLabel.text = ts.Days.ToString();
            }
            else
            {
                yuekaRemainDays.SetActive(false);
            }
        }
        /// <summary>
        /// 是否购买了月卡
        /// </summary>
        void CanBuyMonthCard() 
        {
            long monthCardEndTime = StaticData.playerData.monthCardEndTime;//服务器发来的月卡结束时间
            DateTime endTime = ServerTimerTool.Java2CSTime(monthCardEndTime);
            DateTime currentTime = ServerTimerTool.Java2CSTime(ServerTimerTool.CurrentUTCJavaTime());
            TimeSpan ts = endTime.Subtract(currentTime);//从今天到月卡到期时间还有xxx时间

            if (ts.Days > 5)//剩余5天的时候可以继续购买，还没到时间,不能购买月卡
            {
                MonthCardButtonActive();
                yuekaReceiveOver.SetActive(true);
            }
            else if (ts.Days <= 5 && ts.Days > 0)//在5天之内了,可以购买
            {
                MonthCardButtonActive();
                yuekaConinueBuy.SetActive(true);
            }
            else //if (ts.Days <= 0)//月卡过期了
            {
                MonthCardButtonActive();
                yuekaCanBuy.SetActive(true);
            }
        }
        /// <summary>
        /// 获取月卡信息
        /// </summary>
        void AskChargeList()
        {
            CSGetPaymentInfo msg = new CSGetPaymentInfo();
            msg.channelId = "channelId";
            msg.shopId = "shopId";
            NetAPI.SendHttp(OpDefine.CSGetPaymentInfo, msg, AskChargeListCallBack);
        }
        void AskChargeListCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCGetPaymentInfo msg = packet.kBody as SCGetPaymentInfo;
                if (msg != null)
                {
                    InitPayMentInfo(msg.paymentInfoList);
                }
            }
        }
        /// <summary>
        /// 显示每天领取的钻石
        /// </summary>
        /// <param name="paymentInfoList"></param>
        void InitPayMentInfo(List<PaymentInfo> paymentInfoList)
        {
            foreach (var item in paymentInfoList)
            {
                if (item.paymentType == "month")
                {
                    receiveMonthCard = item.free.ToString();
                }
                else if (item.paymentType == "permanent")
                {
                    receiveZhongShen = item.free.ToString();
                }
            }
            InitLabel();
        }
      
        void CanBuyPerment()
        {
            if (StaticData.playerData.permentType != 1)//没购买终身月卡
            {
                PermentButtonActive();
                zhongshenCanBuy.SetActive(true);
            }
            else
            {
                PermentButtonActive();
                zhongshenReceiveOver.SetActive(true);
            }

        }
        void PermentButtonActive() 
        {
            zhongshenCanBuy.SetActive(false);
            zhongshenCanReceive.SetActive(false);
            zhongshenReceiveOver.SetActive(false);
        }
        void MonthCardButtonActive()
        {
            yuekaCanBuy.SetActive(false);
            yuekaCanReceive.SetActive(false);
            yuekaReceiveOver.SetActive(false);
            yuekaConinueBuy.SetActive(false);
        }
        /// <summary>
        /// 购买,打开充值
        /// </summary>
        /// 
        public void BuyCard()
        {
            PageManager.Instance.OpenPage("ChargePage", "");
        }
      
        /// <summary>
        /// 领月卡
        /// </summary>
        public void ReceiveMonthCard()
        {
            ReceiveCard("month");
            MonthCardButtonActive();
            yuekaReceiveOver.SetActive(true);
            
            if(!yuekaCanReceive.activeInHierarchy && !zhongshenCanReceive.activeInHierarchy)
            {
                StaticData.redTips.SetPointState(RedPointTip.Red.monthCardReward, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }
        public void ReceivePermanent()
        {
            ReceiveCard("permanent");
            PermentButtonActive();
            zhongshenReceiveOver.SetActive(true);
            if (!yuekaCanReceive.activeInHierarchy && !zhongshenCanReceive.activeInHierarchy)
            {
                StaticData.redTips.SetPointState(RedPointTip.Red.monthCardReward, 0);
                StaticData.redTips.ReduceActivitys();
            }
        }
        /// <summary>
        /// 领月卡协议
        /// </summary>
        void ReceiveCard(string cardName)
        {
            CSClaimCard msg = new CSClaimCard();
            msg.cardType = cardName;
            NetAPI.SendHttp(OpDefine.CSClaimCard, msg, ReceiveCardCallBack);
        }

        void ReceiveCardCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCClaimCard msg = packet.kBody as SCClaimCard;
                if (msg != null)//领完月卡了
                {
                   // CanClaimedCard();
                    
                }
            }
        }
      
        /// <summary>
        /// 是否可以领月卡
        /// </summary>
        void CanClaimedCard()
        {
            CSIsCardClaimed msg = new CSIsCardClaimed();
            NetAPI.SendHttp(OpDefine.CSIsCardClaimed, msg, CanClaimedCardCallBack);
        }
        void CanClaimedCardCallBack(NetWork.Packets.Packet packet, bool bSuccess)
        {
            if (bSuccess)
            {
                SCIsCardClaimed msg = packet.kBody as SCIsCardClaimed;
                if (msg != null)
                {
                    if (msg.monthStatus == "canClaim")//可以领取
                    {
                        MonthCardButtonActive();
                        yuekaCanReceive.SetActive(true);
                    }
                    else//不可以领取
                    {
                        MonthCardButtonActive();
                       // yuekaReceiveOver.SetActive(true);
                        CanBuyMonthCard();
                    }
                   
                    if (msg.permanentStatus == "canClaim")//终身月卡可以领取
                    {
                        PermentButtonActive();
                        zhongshenCanReceive.SetActive(true);
                    }
                    else//不可以领取
                    {
                        CanBuyPerment();
                    }
                }
            }
        }

    }
}